package com.ants.modules.quartz.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ants.common.constant.CacheConstant;
import com.ants.modules.utils.ElasticSearchUtils;
import com.ants.modules.ArticleManage.dto.ArticleManageDto;
import com.ants.modules.ArticleManage.entity.ArticleManage;
import com.ants.modules.ArticleManage.service.ArticleManageService;
import com.ants.modules.system.entity.SysUser;
import com.ants.modules.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时发布文章
 */
@Slf4j
public class QuartzPushArticleJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    @Autowired
    ArticleManageService articleManageService;
    @Autowired
    AmqpTemplate rabbitTemplate;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    ElasticSearchUtils elasticSearchUtils;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public static void main(String[] args) {
        String substring = DateUtil.formatDateTime(new DateTime()).substring(0, 16);
        System.out.println(substring);
    }

    /**
     * 0 0/15 * * * ? *
     *
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format("welcome %s! Ants 定时发布文章 QuartzPushArticleJob !   时间:" + DateUtil.now(), this.parameter));
        LambdaQueryWrapper<ArticleManage> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ArticleManage::getPublishState, "2");
        lqw.eq(ArticleManage::getDelFlag, "0");
        List<ArticleManage> list = articleManageService.list(lqw);
        Map<String, String> map = null;
        for (ArticleManage articleManage : list) {
//            String publishTime = DateUtil.formatDateTime(articleManage.getPublishTime()).substring(0, 16);
//            String toDateTime = DateUtil.formatDateTime(new DateTime()).substring(0, 16);
            Date publishTime = articleManage.getPublishTime();
            DateTime toDateTime = DateUtil.date();
            if (toDateTime.compareTo(publishTime) >= 0) {
                articleManage.setPublishState("1");
                articleManageService.updateById(articleManage);
                LambdaQueryWrapper<SysUser> userlqw = new LambdaQueryWrapper<>();
                userlqw.eq(SysUser::getUsername, articleManage.getCreateBy());
                SysUser sysUser = sysUserService.getOne(userlqw);
                if (sysUser != null && StrUtil.isNotBlank(sysUser.getEmail())) {
                    map = new HashMap<>();
                    map.put("addressee", sysUser.getEmail());
                    map.put("id", articleManage.getId());
                    map.put("title", articleManage.getTitle());
                    map.put("publishTime", DateUtil.formatDateTime(publishTime));
                    rabbitTemplate.convertAndSend("sendQuartzPushMessageQueue", map);
                }
                // 将数据加入 es
                ArticleManageDto docMap = articleManageService.getArticleToESById(articleManage.getId());
                elasticSearchUtils.addDocument(CacheConstant.ES_INDEX, articleManage.getId(), docMap);
            }
        }


    }
}
